package com.welove.activity.login;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.welove.R;
import com.welove.activity.BaseActivity;
import com.welove.asynctask.TAsyncTask;
import com.welove.util.LogFactory;
import com.welove.util.ValueUtil;

/**
 * @author dreamfutureba@gmail.com
 * @date 2013-11-3下午8:44:58
 * @version
 * @description 登陆界面
 */
public class LoginActivity extends BaseActivity {
	private static final String TAG = LoginActivity.class.getSimpleName();

	private EditText login_username, login_password;
	private CheckBox login_remember_me;
	private Button login_confirm;

	private TAsyncTask loginTask;

	private String username, password;

	@Override
	protected void installViews() {
		setContentView(R.layout.activity_login);
		initView();
	}

	private void initView() {
		login_username = initEt(R.id.login_et_username);
		login_password = initEt(R.id.login_et_password);
		login_remember_me = initCb(R.id.login_check_remeber_me);
		login_confirm = initBtn(R.id.login_btn_confirm);
	}

	@Override
	protected void initTitleBar() {
		super.initTitleBar();
		mTitleBar.titlebar_tv_center.setText("登录");
		mTitleBar.titlebar_btn_right.setText("免费注册");
	}

	@Override
	protected void registerEvents() {
		login_remember_me
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

					}
				});

		login_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				username = login_username.getText().toString();
				password = login_password.getText().toString();
				LogFactory.e(TAG, "username=" + username);
				LogFactory.e(TAG, "password=" + password);
				if (checkData()) {
					requestLogin(username, password);
				}
			}
		});
	}

	@Override
	public void onBeforeTask(int taskId) {
		if (taskId == 100001) {
			// 弹出进度条
		}
	}

	@Override
	public Object onTask(int taskId, Object... params) throws Exception {
		// String result = "";
		// HashMap<String, Object> map = new HashMap<String, Object>();
		// if (taskId == 100001) {
		// map.put("username", params[0].toString());
		// map.put("password", params[1].toString());
		// // result = HttpManager.sendPostRequest(UrlConstant.LOGIN_URL,
		// map,"utf-8");
		// }
		// return result;
		return null;
	}

	@Override
	public void onAfterTask(int taskId, Object result) {
		super.onAfterTask(taskId, result);
	}

	@Override
	public void onTaskError(int taskId, Exception exception) {
		// 关闭进度条
	}

	/**
	 * 登陆验证
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 */
	private void requestLogin(String username, String password) {
		if (loginTask != null) {
			loginTask = null;
		}
		// 登陆
		loginTask = new TAsyncTask(LoginActivity.this);
		loginTask.setTaskID(100001);
		loginTask.execute(username, password);
	}

	private boolean checkData() {
		if (ValueUtil.isStrEmpty(username) || ValueUtil.isStrEmpty(password)) {
			return false;
		}
		return true;
	}
}
